package ui2.player
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.MouseEvent;
   // greensock
   import com.greensock.TweenMax;
   import com.greensock.easing.Linear;
   // boy
   import ui2.MyPlayerMgr;

   /**
    * @author     cjboy | cjboy1984@gmail.com
    * @usage
    * 1. 首先，你必需先有個MyPlayerMgr的getter，以範例來說是取得第0個MyPlayerMgr。
    * private function get mgr():MyPlayerMgr { return MyPlayerMgr.getMgrAt(0); }
    *
    * 2. 再來利用mgr來監聽事件和用mgr來執行行為。
    * mgr.play();
    * mgr.pause();
    * ...
    */
   public class SoundBar_1 extends MovieClip
   {
      // whether to resume play
      private var resumePlay:Boolean;

      // percentage of the seeker
      private var seekToPerc:Number;

      /* constructor */
      public function SoundBar_1()
      {
         // disable tab-functionality.
         tabEnabled = false;
         tabChildren = false;
         focusRect = false;

         buttonMode = true;
         mouseChildren = false;

         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // ------------------ LINE -------------------

      // ################ Protected ################

      // ################# Private #################

      private function onAdd(e:Event):void
      {
         // seeker functionality
         addEventListener(MouseEvent.MOUSE_DOWN, onMDown);
      }

      private function onRemove(e:Event):void
      {
         // seeker functionality
         removeEventListener(MouseEvent.MOUSE_DOWN, onMDown);
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMMove);
         stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
      }

      // ------------------ LINE -------------------

      private function onMDown(e:MouseEvent):void
      {
         // new volume
         mgr.volumePercentage = getVolumePercentage(mouseX / width);

         // add drag & up handler
         stage.addEventListener(MouseEvent.MOUSE_MOVE, onMMove);
         stage.addEventListener(MouseEvent.MOUSE_UP, onMUp);
      }

      private function onMMove(e:MouseEvent):void
      {
         // new volume
         mgr.volumePercentage = getVolumePercentage(mouseX / width);
      }

      private function onMUp(e:MouseEvent):void
      {
         // new volume
         mgr.volumePercentage = getVolumePercentage(mouseX / width);

         // remove drag & up handler
         stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMMove);
         stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
      }

      // ------------------ LINE -------------------

      private function getVolumePercentage(v:Number):int
      {
         if (v <= 0)
         {
            gotoAndStop(6);
            return 0;
         }
         else if (v < 0.2)
         {
            gotoAndStop(5);
            return 20;
         }
         else if (v < 0.4)
         {
            gotoAndStop(4);
            return 40
         }
         else if (v < 0.6)
         {
            gotoAndStop(3);
            return 60;
         }
         else if (v < 0.8)
         {
            gotoAndStop(2);
            return 80;
         }
         else
         {
            gotoAndStop(1);
            return 100;
         }
      }

      // ------------------ LINE -------------------

      private function get mgr():MyPlayerMgr { return MyPlayerMgr.getMgrAt(0); }

      // ------------------ LINE -------------------

   }

}